home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / stdlib / atoi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-09  |  752 b   |  46 lines

  1.  
  2. /*
  3.  *  int ATOI(str)
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <stdlib.h>
  11.  
  12. int
  13. atoi(str)
  14. const char *str;
  15. {
  16.     short neg = 0;
  17.     long v = 0;
  18.  
  19.     while (*str == ' ' || *str == '\t')
  20.     ++str;
  21.     if (*str == '-') {
  22.     neg = 1;
  23.     ++str;
  24.     }
  25.     if (*str == '+')
  26.     ++str;
  27.  
  28.     /*
  29.      *    while v < 65536 we setup the operation such that it is optimizable
  30.      *    by DICE.  This yields a huge efficiency increase.
  31.      */
  32.  
  33.     while (*str >= '0' && *str <= '9') {
  34.     if (v <= (unsigned short)-1)
  35.         v = (unsigned short)v * 10;
  36.     else
  37.         v = v * 10;
  38.     v += *str++ - '0';
  39.     }
  40.     if (neg)
  41.     v = -v;
  42.     return(v);
  43. }
  44.  
  45.  
  46.